package com.ztz.mynewdemo.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.ztz.mynewdemo.R;
import com.ztz.mynewdemo.bean.TodayDetailResponse;
import com.ztz.mynewdemo.utils.NewsApi;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TodayDetailActivity extends BaseActivity {
    public static final String DETAIL_ID = "id";
    @BindView(R.id.toDetail_iv)
    ImageView toDetailIv;
    @BindView(R.id.detail_title)
    TextView detailTitle;
    @BindView(R.id.detail_des)
    TextView detailDes;
    @BindView(R.id.detail_content)
    TextView detailContent;
    @BindView(R.id.detail_year)
    TextView detailYear;
    @BindView(R.id.detail_month)
    TextView detailMonth;
    @BindView(R.id.detail_day)
    TextView detailDay;
    @BindView(R.id.detail_lunar)
    TextView detailLunar;
    FloatingActionButton fab;
    ScrollView scrollView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_today_detail);
        ButterKnife.bind(this);
        fab=(FloatingActionButton) findViewById(R.id.top);
        scrollView=(ScrollView)findViewById(R.id.sc_detail);
        setToolbar("历史上的今天");
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Handler().post(new Runnable() {
                    @Override
                    public void run() {
                        scrollView.fullScroll(ScrollView.FOCUS_UP);
                    }
                });
            }
        });

        String id = getIntent().getStringExtra(DETAIL_ID);
        getData(id);
    }

    private void getData(final String id) {
        OkGo.<String>get(NewsApi.TODAY_DETAIL)
                .tag(this)
                .params("key", "907298e415a8318e78b1cb4a3403c01e")
                .params("v", 1.0)
                .params("id", id)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String s = response.body();
                        Gson gson = new Gson();
                        TodayDetailResponse response1 = gson.fromJson(s, TodayDetailResponse.class);
                        Glide.with(TodayDetailActivity.this).load(response1.getResult().get(0).getPic()).into(toDetailIv);
                        detailTitle.setText(response1.getResult().get(0).getTitle());
                        detailDes.setText(response1.getResult().get(0).getDes());
                        detailContent.setText(response1.getResult().get(0).getContent());
                        detailYear.setText(response1.getResult().get(0).getYear()+"年");//拿到第1条数据
                        detailMonth.setText(response1.getResult().get(0).getMonth()+"月");
                        detailDay.setText(response1.getResult().get(0).getDay()+"日");
                        detailLunar.setText(response1.getResult().get(0).getLunar());
                    }
                });

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
